from typing import Optional, List
from app.core.crud import CRUDBase
from app.models.system.blog import Blog
from app.schemas.system.blog import BlogCreate, BlogUpdate,BlogResponse
from tortoise.expressions import Q

class BlogService(CRUDBase[Blog, BlogCreate, BlogUpdate]):
    def __init__(self):
        super().__init__(model=Blog)

    async def get_blog_by_user(self, blog_id: int, user_id: int= None) -> Optional[Blog]:
        """根据博客ID获取博客"""
        blog = await self.model.filter(id=blog_id, is_deleted=False).prefetch_related('user').first()
        return blog
        # return BlogResponse.model_validate(blog)

    async def list_blogs_by_user(self, user_id: int, page: int = 1, page_size: int = 10) -> List[Blog]:
        """根据用户ID获取博客列表"""
        return await self.model.filter(user_id=user_id, is_deleted=False).offset((page-1)*page_size).limit(page_size)

blog_service = BlogService()